package clue.board;

import java.io.InputStream;
import java.util.HashMap;
import java.util.Scanner;

import clue.board.Square.SquareType;
import clue.enums.Room;
import clue.players2.Player2;

/**
 *
 * @author Shawn
 */
public class Board {
	//
	// Want something that represents the configuration of a square.
	// No borders, top, right, bottom, left... That's 4 bits of information.
	// So a byte is enough: abcd
	//   a: top
	//   b: right
	//   c: bottom
	//   d: left
	//
	
	public final int rows;
	public final int cols;
	
	public Square[][] board;
	public Square[] startingLocs;
	public HashMap<Room, Square> roomToSquare;
	
	public Board() {
		Scanner scanner = null;
		InputStream is = this.getClass().getClassLoader().getResourceAsStream("resources/default_board");
		scanner = new Scanner(is);
		rows = scanner.nextInt();
		cols = scanner.nextInt();
		scanner.nextLine();
		board = new Square[rows][cols];
		roomToSquare = new HashMap<>();
		for (int i = 0; i < rows; i++) {
			String line = scanner.nextLine();
			assert(line.length() == cols);
			for (int j = 0; j < cols; j++) {
				board[i][j] = new Square(i, j, line.substring(j, j+1));
			}
		}
		for (int i = 0; i < rows; i++) {
			for (int j = 0; j < cols; j++) {
				if (!board[i][j].tp) {
					assert(i > 0 && !board[i-1][j].bm);
					board[i][j].neighbors.add(board[i-1][j]);
				}
				if (!board[i][j].rt) {
					assert(j < (cols - 1) && !board[i][j+1].lt);
					board[i][j].neighbors.add(board[i][j+1]);
				}
				if (!board[i][j].bm) {
					assert(i < (rows - 1) && !board[i+1][j].tp);
					board[i][j].neighbors.add(board[i+1][j]);
				}
				if (!board[i][j].lt) {
					assert(j > 0 && !board[i][j-1].rt);
					board[i][j].neighbors.add(board[i][j-1]);
				}
			}
		}
		scanner.close();
		is = this.getClass().getClassLoader().getResourceAsStream("resources/default_rooms");
		scanner = new Scanner(is);
		for (int i = 0; i < rows; i++) {
			String line = scanner.nextLine();
			for (int j = 0; j < cols; j++) {
				int room = Integer.parseInt(line.substring(j, j+1)) - 1;
				if (room >= 0) {
					board[i][j].room = Room.values()[room];
					board[i][j].type = SquareType.ROOM;
					roomToSquare.put(board[i][j].room, board[i][j]);
				} else {
					board[i][j].type = SquareType.REGULAR;
				}
			}
		}
		scanner.close();
		startingLocs = new Square[6];
		startingLocs[0] = board[0][14];
		startingLocs[1] = board[6][23];
		startingLocs[2] = board[24][16];
		startingLocs[3] = board[24][7];
		startingLocs[4] = board[17][0];
		startingLocs[5] = board[7][0];
	}
	
	public String toString() {
		StringBuilder sb = new StringBuilder();
		for (int i = 0; i < rows; i++) {
			for (int j = 0; j < cols; j++) {
				sb.append('+');
				sb.append(board[i][j].tp ? '-' : ' ');
			}
			sb.append('+');
			sb.append('\n');
			for (int j = 0; j < cols; j++) {
				sb.append(board[i][j].lt ? '|' : ' ');
				sb.append(' ');
			}
			sb.append(board[i][cols-1].rt ? '|' : ' ');
			sb.append('\n');
		}
		for (int j = 0; j < cols; j++) {
			sb.append('+');
			sb.append(board[rows-1][j].bm ? '-' : ' ');
		}
		sb.append('+');
		return sb.toString();
	}
	
	public String string(Integer player, HashMap<Integer, Square> playerLocation) {
		StringBuilder sb = new StringBuilder();
		HashMap<Square, Character> squareMap = new HashMap<>();
		for (Integer p : playerLocation.keySet()) {
			if (p.equals(player)) {
				squareMap.put(playerLocation.get(p), 'X');
			} else {
				squareMap.put(playerLocation.get(p), 'Y');
			}
		}
		for (int i = 0; i < rows; i++) {
			for (int j = 0; j < cols; j++) {
				sb.append('+');
				sb.append(board[i][j].tp ? '-' : ' ');
			}
			sb.append('+');
			sb.append('\n');
			for (int j = 0; j < cols; j++) {
				sb.append(board[i][j].lt ? '|' : ' ');
				Character c = squareMap.get(board[i][j]);
				sb.append(c == null ? ' ' : c);
			}
			sb.append(board[i][cols-1].rt ? '|' : ' ');
			sb.append('\n');
		}
		for (int j = 0; j < cols; j++) {
			sb.append('+');
			sb.append(board[rows-1][j].bm ? '-' : ' ');
		}
		sb.append('+');
		return sb.toString();
		
	}
	
	public static void main(String[] args) {
		Board b = new Board();
		System.out.println(b);
	}
	
}
